package main

import (
	"fmt"
	"sync"
	"time"
)

var piao = 10
var wg1 sync.WaitGroup
var cl sync.Mutex
func main() {
	wg1.Add(4)
	go ck(1)
	go ck(2)
	go ck(3)
	go ck(4)
	fmt.Println("窗口开始卖票中。。。。。。")
	wg1.Wait()
	fmt.Println("main，解除阻塞。。")
}

func ck(k int) {
	defer wg1.Done()
	for  {
		cl.Lock()
		if piao>0 {
			time.Sleep(150*time.Millisecond)
			fmt.Println("窗口",k,"正在卖第",piao,"张")
			piao--

		}else{
			cl.Unlock()
			fmt.Println("窗口",k,"卖空了，没的卖了，请换其它窗口")
			break
		}
		cl.Unlock()
	}
}
